home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.12 Dec 96 / Async I⁄O Code / Sources / AsyncPrefsDLOG.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-10  |  9.0 KB  |  393 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    Dialog Module, created by Resorcerer
  3.  */
  4.  
  5. #if 0
  6. #include <Desk.h>
  7. #include <DiskInit.h>
  8. #include <Dialogs.h>
  9. #include <Files.h>
  10. #include <Fonts.h>
  11. #include <Lists.h>
  12. #include <Menus.h>
  13. #include <Resources.h>
  14. #include <Memory.h>
  15. #include <OSUtils.h>
  16. #include <OSEvents.h>
  17. #include <Packages.h>
  18. #include <Scrap.h>
  19. #include <Script.h>
  20. #include <SegLoad.h>
  21. #include <stdio.h>
  22. #include <ToolUtils.h>
  23. #include <Values.h>
  24. #endif
  25.  
  26. #include "AsyncPrefsDialog.h"
  27. #include "ResorcererDialogLib.h"
  28.  
  29. long    gNumIterations = 10;
  30. Str63    gNameString = "\pResults";
  31. Boolean    gRunRandom = true;
  32.  
  33. /* Symbolic Dialog Item Numbers */
  34.  
  35. static enum {
  36.     BUT1_OK = 1,
  37.     BUT2_Cancel,
  38.     dIterText,
  39.     dNameText,
  40.     dRandomBut,
  41.     STXT6_Run,
  42.     STXT7_times,
  43.     STXT8_Put,
  44.     LASTITEM
  45.     };
  46.  
  47. #define OK_ITEM     BUT1_OK
  48. #define CANCEL_ITEM     BUT2_Cancel
  49.  
  50. /* Useful constants */
  51.  
  52. /* Prototypes */
  53.  
  54.  
  55. pascal  Boolean MyFilter(DialogPtr dlog, EventRecord *evt, short *itemHit);
  56. Boolean CheckUserItems(Point where, short *itemHit);
  57. int     AnyBadValues(DialogPtr dlog);
  58.  
  59. extern    Boolean        gModalDialogActive;
  60. extern    DialogPtr    gModalDialog;
  61.  
  62. static Point where;
  63. static int modifiers;
  64.  
  65. /*
  66.  *    For non-modal dialogs, after a mouse down event in dialog window, w, call this
  67.  *    to entertain the click.
  68.  */
  69.  
  70. #define kNameStrings 512
  71. #define kPrefsID     128
  72. struct PrefsResource {
  73.     short    numIterations;
  74.     short    randomize;
  75.     Str63    fileName;
  76. };
  77.  
  78. typedef struct PrefsResource PrefsResource, **PrefsHandle;
  79.  
  80. static OSErr GetPrefsFile(FSSpec *prefsFileSpec);
  81.  
  82. static OSErr GetPrefsFile(FSSpec *prefsFileSpec)
  83. {
  84.     OSErr        err;
  85.     short        vRefNum;
  86.     long        dirID;
  87.     Str255        prefsFileName;
  88.     
  89.     err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder, &vRefNum, &dirID);
  90.     if (err == noErr) {
  91.         GetIndString(prefsFileName, kNameStrings, 1);
  92.         err = FSMakeFSSpec(vRefNum, dirID, prefsFileName, prefsFileSpec);
  93.     }
  94.     return err;
  95. }
  96.  
  97. void ReadAsyncPrefs()
  98. {
  99.     OSErr        err;
  100.     FSSpec        prefsFileSpec;
  101.     short        refNum = -1;
  102.     PrefsHandle    prefs;
  103.     
  104.     // Try to get the preferences information from a prefs file. If none is
  105.     // found, get the default preferences from the application's resource fork
  106.     err = GetPrefsFile(&prefsFileSpec);
  107.     if (err == noErr) {
  108.         refNum = FSpOpenResFile(&prefsFileSpec, fsRdPerm);
  109.     }
  110.     
  111.     // Read in the startup preferences
  112.     prefs = (PrefsHandle)GetResource('APRF', 128);
  113.     if (prefs == nil) {
  114.         // Set up default values 'cause something really went wrong
  115.         gNumIterations = 10;
  116.         gRunRandom = true;
  117.         BlockMove((Ptr)"\pResults", gNameString, 8);
  118.     } else {
  119.         gNumIterations = (**prefs).numIterations;
  120.         gRunRandom = (**prefs).randomize;
  121.         BlockMove((Ptr)&((**prefs).fileName), (Ptr)gNameString, sizeof(Str63));
  122.         ReleaseResource((Handle)prefs);
  123.     }
  124.     
  125.     if (refNum != -1)
  126.         CloseResFile(refNum);
  127. }
  128.  
  129. void WriteAsyncPrefs()
  130. {
  131.     OSErr        err;
  132.     FSSpec        prefsFileSpec;
  133.     short        homeRefNum,
  134.                 refNum = -1;
  135.     PrefsHandle    prefs;
  136.     
  137.     // Try to get the preferences information from a prefs file. If none is
  138.     // found, get the default preferences from the application's resource fork
  139.     err = GetPrefsFile(&prefsFileSpec);
  140.     if (err == fnfErr) {
  141.         DebugStr("\pCReating prefs file");
  142.         FSpCreateResFile(&prefsFileSpec, 'RDC1', 'PREF', smRoman);
  143.         err = ResError();
  144.         if (err != noErr) goto done;
  145.     }
  146.     refNum = FSpOpenResFile(&prefsFileSpec, fsRdWrPerm);
  147.     if (refNum == -1) goto done;
  148.     
  149.     // Read in the current preferences resource
  150.     prefs = (PrefsHandle)GetResource('APRF', 128);
  151.     if (prefs == nil) goto done;     // We can't find the prefs resource in our preferences
  152.                                     // file or in the application's resource fork. This is bad.
  153.     
  154.     // If this resource isn't in the prefs file, add it
  155.     homeRefNum = HomeResFile((Handle)prefs);
  156.     if (homeRefNum == -1) goto done;    // Not a handle to a resource!
  157.  
  158.     if (homeRefNum != refNum) {
  159.         // The resource came from our application, so copy it into the prefs file
  160.         DetachResource((Handle)prefs);
  161.         UseResFile(refNum);
  162.         AddResource((Handle)prefs, 'APRF', 128, "\pPreferences");
  163.         ChangedResource((Handle)prefs);
  164.         UpdateResFile(refNum);
  165.     }
  166.     
  167.     // Update the contents of the prefs resource and mark it as changed
  168.     (**prefs).numIterations = gNumIterations;
  169.     (**prefs).randomize = gRunRandom;
  170.     BlockMove((Ptr)gNameString, (Ptr)&((**prefs).fileName), sizeof(Str63));
  171.     ChangedResource((Handle)prefs);
  172.     
  173. done:
  174.     if (refNum != -1)
  175.         CloseResFile(refNum);
  176. }
  177.  
  178. void DoDialogContent(DialogPtr w, EventRecord *evt)
  179.     {
  180.         short itemHit;
  181.         
  182.         SetPort(w);                        /* If it's not already */
  183.         
  184.         modifiers = evt->modifiers;        /* Stuff our local globals */
  185.         where = evt->where;
  186.         GlobalToLocal(&where);
  187.         
  188.          if (CheckUserItems(where,&itemHit) || DialogSelect(evt,&w,&itemHit))
  189.              if (!DoDialogItem(w,itemHit))
  190.                  CloseThisDialog(w);
  191.     }
  192.  
  193. /*
  194.  * Mouse down event:
  195.  * Check if it's in some user item, and convert to itemHit if appropriate.
  196.  */
  197.  
  198. static Boolean CheckUserItems(Point where, short *itemHit)
  199.     {
  200.         return(FALSE);
  201.     }
  202.  
  203. /*
  204.  * Redraw the contents of this dialog due to update event.
  205.  * If you have not installed UserItem draw routines, you should redraw
  206.  * them explicitly here; otherwise, UpdtDialog() will call your routines.
  207.  */
  208.  
  209. void DoDialogUpdate(DialogPtr dlog)
  210.     {
  211.         GrafPtr oldPort;
  212.  
  213.         GetPort(&oldPort); SetPort(dlog);
  214.         BeginUpdate(dlog);
  215.  
  216.         UpdtDialog(dlog,dlog->visRgn);
  217.         FrameDefault(dlog,BUT1_OK,TRUE);
  218.  
  219.         EndUpdate(dlog);
  220.         SetPort(oldPort);
  221.     }
  222.  
  223. /*
  224.  * Activate event: Activate or deactivate this dialog and any items in it
  225.  */
  226.  
  227. void DoDialogActivate(DialogPtr dlog, int activ)
  228.     {
  229.         SetPort(dlog);
  230.     }
  231.  
  232. /*
  233.  * Build this dialog's window on desktop, and install initial item values.
  234.  * Return the dlog opened, or NIL if error (no resource, no memory).
  235.  */
  236.  
  237. DialogPtr OpenThisDialog()
  238.     {
  239.         short type; Handle hndl; Rect box; GrafPtr oldPort;
  240.         DialogPtr dlog; unsigned char *p,str[256];
  241.  
  242.         GetPort(&oldPort);
  243.         dlog = GetNewDialog(thisDialogID,NIL,FRONT_WINDOW);
  244.         if (dlog == NIL) { SysBeep(1); return(NIL); }    /* Poor man's error message */
  245.  
  246.         CenterWindow(dlog,0);
  247.         SetPort(dlog);
  248.  
  249.         /* Fill in dialog's values here */
  250.  
  251.         PutDlgLong(dlog, dIterText,gNumIterations, TRUE);
  252.         PutDlgString(dlog, dNameText,(void*)gNameString, FALSE);
  253.  
  254.         PutDlgChkRadio(dlog, dRandomBut, gRunRandom);
  255.  
  256.         ShowWindow(dlog);
  257.         return(dlog);
  258.  
  259.     }
  260.  
  261. /*
  262.  * Clean up any allocated stuff, and return dialog to primordial mists
  263.  */
  264.  
  265. void CloseThisDialog(DialogPtr dlog)
  266.     {
  267.         DisposDialog(dlog);    /* Call CloseDialog if you provide storage to GetNewDialog */
  268.         gModalDialogActive = false;
  269.         gModalDialog = NULL;
  270.     }
  271.  
  272. /*
  273.  * Deal with user clicking on an item in this dialog, either modal or non-modal.
  274.  * The local point is in where; modifiers in modifiers.
  275.  * Returns whether or not the dialog should be closed (keepGoing).
  276.  */
  277.  
  278. int DoDialogItem(DialogPtr dlog, short itemHit)
  279.     {
  280.         short type,okay=FALSE,keepGoing=TRUE,val;
  281.         Handle hndl; Rect box; Point pt;
  282.         unsigned char *p,str[256];
  283.  
  284.         if (itemHit<1 || itemHit>=LASTITEM)
  285.             return(keepGoing);                /* Only legal items, please */
  286.  
  287.         GetDItem(dlog,itemHit,&type,&hndl,&box);
  288.         switch(type) {
  289.             case ctrlItem+btnCtrl:
  290.                 switch(itemHit) {
  291.                     case BUT1_OK:
  292.                         keepGoing = FALSE; okay = TRUE;
  293.                         break;
  294.                     case BUT2_Cancel:
  295.                         keepGoing = FALSE;
  296.                         break;
  297.                     }
  298.                 break;
  299.             case ctrlItem+chkCtrl:
  300.                 SetCtlValue((ControlHandle)hndl,val = !GetCtlValue((ControlHandle)hndl));
  301.                 switch(itemHit) {
  302.                     case dRandomBut:
  303.                         break;
  304.                     }
  305.                 break;
  306.             case ctrlItem+radCtrl:
  307.                 break;
  308.             case ctrlItem+resCtrl:
  309.                 break;
  310.             case statText:
  311.                 switch(itemHit) {
  312.                     case STXT6_Run:        /* NOT Enabled */
  313.                         break;
  314.                     case STXT7_times:        /* NOT Enabled */
  315.                         break;
  316.                     case STXT8_Put:        /* NOT Enabled */
  317.                         break;
  318.                     }
  319.                 break;
  320.             case editText:
  321.                 switch(itemHit) {
  322.                     case dIterText:
  323.                         break;
  324.                     case dNameText:
  325.                         break;
  326.                     }
  327.                 break;
  328.             case iconItem:
  329.                 break;
  330.             case picItem:
  331.                 break;
  332.             case userItem:
  333.                 break;
  334.             }
  335.  
  336.         if (okay) {
  337.             keepGoing = AnyBadValues(dlog);
  338.             if (!keepGoing) {
  339.                 // Extract final values
  340.                 GetDlgLong(dlog,dIterText,&gNumIterations);
  341.                 GetDlgString(dlog,dNameText,(char*)gNameString);
  342.                 gRunRandom = (Boolean)GetDlgChkRadio(dlog, dRandomBut);
  343.             }
  344.         }
  345.         return(keepGoing);
  346.     }
  347.  
  348. /*
  349.  * Pull values out of dialog items and deliver TRUE if any of them are
  350.  * illegal or inconsistent; otherwise deliver FALSE.  If any values are bad,
  351.  * you should inform your user about the problem here before delivering TRUE.
  352.  * If any items are missing values, this is the place to assign any defaults.
  353.  */
  354.  
  355. int AnyBadValues(DialogPtr dlog)
  356.     {
  357.         char str[256]; long val;
  358.  
  359.         if (!GetDlgLong(dlog,dIterText,&val)) {
  360.             PutDlgLong(dlog,dIterText,gNumIterations, TRUE);
  361.             return true;
  362.         }
  363.         
  364.         GetDlgString(dlog,dNameText,str);
  365.         if (true) {
  366.             Boolean    changed = false;
  367.             int        count;
  368.             
  369.             if (*str)
  370.                 // Remove any path separators
  371.                 for (count = *str; count >= 1; count--) {
  372.                     if (str[count] == ':') { 
  373.                         str[count] = '-';
  374.                         changed = true;
  375.                     }
  376.                 }
  377.             
  378.             if (changed) {
  379.                 PutDlgString(dlog,dNameText,str, TRUE);
  380.                 return true;
  381.             }
  382.             
  383.             if ((*str == (char)0) || (*str > (char)63)) {
  384.                 PutDlgString(dlog,dNameText,(char*)gNameString, TRUE);
  385.                 return true;
  386.             }
  387.         }
  388.  
  389.         return(FALSE);
  390.     }
  391.  
  392.  
  393.